package com.yunsc.order.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunsc.order.entity.YunscOrderEntity;
import com.yunsc.order.entity.YunscOrderGoodsEntity;
import com.yunsc.order.handler.RedisDelayQueueHandle;
import com.yunsc.order.handler.exception.DelayExceptionHandler;
import com.yunsc.order.sevice.YunscOrderGoodsService;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.product.service.YunscGoodsService;
import com.yunsc.store.service.YunscUserCouponService;
import com.yunsc.store.service.YunscUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 订单支付超时处理类
 * @date: 2024-05-25 15:44
 */
@Component("orderPaymentTimeout")
@Slf4j
public class OrderPaymentTimeout implements RedisDelayQueueHandle<Map> {

    @Resource
    private YunscOrderService orderService;
    @Resource
    private YunscOrderGoodsService orderGoodsService;
    @Resource
    private YunscGoodsService goodsService;
    @Resource
    private YunscUserService userService;
    @Resource
    private YunscUserCouponService userCouponService;

    @Override
    @Transactional
    @DelayExceptionHandler
    public void execute(Map map) {
        log.info("(收到订单支付超时延迟消息) {}", map);
        log.info("(订单支付超时，自动取消订单处理业务...) {}", map);
        // 订单支付超时，自动取消订单处理业务...
        // map.put("orderId", orderEntity.getOrderId());
        // map.put("remark", "订单支付超时，自动取消订单");
        Integer orderId = (Integer) map.get("orderId");

        // 更新订单状态为取消状态
        YunscOrderEntity orderEntity = orderService.getById(orderId);
        if (orderEntity == null) {
            log.error("(订单不存在) {}", map);
            return;
        }

        orderEntity.setOrderStatus(20);
        orderEntity.setIsDelete(1);
        orderService.updateById(orderEntity);
        // orderService.update().eq("order_id", orderId).set("order_status", 20).set("is_delete", 1).update();

        // 返还商品库存
        LambdaQueryWrapper<YunscOrderGoodsEntity> lqw = new LambdaQueryWrapper<YunscOrderGoodsEntity>().eq(YunscOrderGoodsEntity::getOrderId, orderId);
        List<YunscOrderGoodsEntity> orderGoodsList = orderGoodsService.list(lqw);
        orderGoodsList.forEach(orderGoods -> {
            // 返还商品库存 (库存计算方式(10下单减库存 20付款减库存))
            if (orderGoods.getDeductStockType() == 10){
                // 下单减库存
                log.info("(订单支付超时，返还商品库存) {}", map);
                goodsService.addGoodsStock(orderGoods.getGoodsId(), orderGoods.getGoodsSkuId(), orderGoods.getTotalNum());
            }
        });


        // 判断订单是否支付积分
        Integer pointsNum = orderEntity.getPointsNum();
        if (pointsNum != null && pointsNum > 0) {
            // 返还用户积分
            log.info("(订单支付超时，返还用户积分) {}", map);
            userService.updateUserPoints(orderEntity.getUserId(), pointsNum, "订单：" + orderEntity.getOrderNo() + "支付超时返还积分");
        }

        // 判断订单是否支付优惠券
        Integer couponId = orderEntity.getCouponId();
        if (couponId != null && couponId > 0) {
            // 返还用户优惠券
            log.info("(订单支付超时，返还用户优惠券) {}", map);
            userCouponService.update().eq("user_id", orderEntity.getUserId()).eq("coupon_id", couponId)
                    .set("is_use", 0).update();
        }

        log.info("(订单超时未支付，订单自动取消处理完成) {}", map);
    }
}

